<?php

/**
 * EcommercePanelController is the default controller for this application
 */
class EcommercePanelController extends BrowseControllerBase {
	
	/**
	 * Constructor of index
	 */
	public function __construct() {
		parent::__construct ();
		
		//Check Correct User Login
		$this->requireUserLogin ();
		
		/*
		//Check if User is logged in
		$currentUserId = $this->authentication->getCurrentUserId();
		if ($currentUserId == null) {
			// Go on to login page
			$url = Zend_Registry::get('config')->APPLICATION_URL . '/Browse/';
			CommonUtils::redirect($url);
			return -1;
		}
		*/
		
		//Check if Blogging Module is enabled
		if ($this->currentPackageOption) {
			$module = MODULE_ECOMMERCE;
			$isEnabled = $this->currentPackageOption->isEnabled ( $module );
			if ($isEnabled == false) {
				$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Browse/Index/errorAuthor';
				CommonUtils::redirect ( $url );
				return - 1;
			}
		}
	
	}
	
	/**
	 * The "index" action is the default action for all controllers -- the
	 * landing page of the site.
	 *
	 * @return void
	 *
	 */
	public function indexAction() {
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/list_ecommerce.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
	
	/**
	 * Handle General Settings for Ecommerce Panel
	 */
	public function generalAction() {
		$shippingFeeSettingEntity = $this->settingsDataService->getItem ( SETTINGS_SHIPPING_FEE, $this->authentication->getCurrentUserId () );
		$shippingMethodSettingEntity = $this->settingsDataService->getItem ( SETTINGS_SHIPPING_METHOD, $this->authentication->getCurrentUserId () );
		$currencySettingEntity = $this->settingsDataService->getItem ( SETTINGS_USER_CURRENCY, $this->authentication->getCurrentUserId () );
		
		// Get action method and check it
		$actionForm = $this->getPost ( 'actionForm', '' );
		$errorInfoMessage = array ();
		
		// Only Normal User or Admin User can update this information
		if (! empty ( $actionForm ) && $actionForm == 'submit' && $this->isDemoMode == false) {
			// Update FB, TW and Linkin Link
			/*$shipping_fee = $this->getPost('shipping_fee', "");
			$shipping_fee = floatval($shipping_fee);			
			$shippingFeeSettingEntity->value = $shipping_fee;
			$this->settingsDataService->update($shippingFeeSettingEntity);
			
			// Update FB, TW and Linkin Link
			$shipping_method = $this->getPost('shipping_method', "");
			$shipping_method = strval($shipping_method);			
			$shippingMethodSettingEntity->value = $shipping_method;
			$this->settingsDataService->update($shippingMethodSettingEntity);
			*/
			// Update Currency Value
			$currency = $this->getPost ( 'currency', "" );
			$currencySettingEntity->value = $currency;
			$this->settingsDataService->update ( $currencySettingEntity );
		}
		// Assign value of shipping Fee
		//$shipping_fee = floatval($shippingFeeSettingEntity->value);
		//$shipping_method = $shippingMethodSettingEntity->value;
		$currency = $currencySettingEntity->value;
		//$this->smarty->assign('shipping_fee', $shipping_fee);
		//$this->smarty->assign('shipping_method', $shipping_method);
		$this->smarty->assign ( 'currency', $currency );
		
		$this->smarty->assign ( 'SMARTY_CONTENT_PATH', 'Browse/EcommercePanel/general_settings.tpl' );
		$this->smarty->display ( APPLICATION_PATH . '/Layouts/Browse/MainLayout.tpl' );
	}
}
